package com.wenlin.myproduction.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.wenlin.myproduction.R;
import com.wenlin.myproduction.base.BaseActivity;
import com.wenlin.myproduction.base.BaseSwipeActivity;
import com.wenlin.myproduction.bean.ActivityBean;
import com.wenlin.myproduction.bean.User;
import com.wenlin.myproduction.util.ToastUtils;
import com.wenlin.myproduction.util.UserManage;
import com.wenlin.myproduction.util.ValCommon;
import com.wenlin.myproduction.widget.LotteryCircleView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;

public class LotteryCircleViewActivity extends BaseSwipeActivity {
    @BindView(R.id.lv)
    LotteryCircleView lotteryView;
    @BindView(R.id.tv_coupon_count)
    TextView tvCouponCount;

    private List<ActivityBean.ActivityGiftBean> gifts;

    @Override
    public int getContentView() {
        return R.layout.activity_lottery_circle_view;
    }

    public static void start(BaseActivity activity) {
        Intent intent = new Intent(activity, LotteryCircleViewActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(ValCommon.LOTTERY_CIRCLE_VIEW);
        User user = UserManage.getInstance().getUser();
        if (user.CouponCount <= 0) {
            user.CouponCount += 1;
            UserManage.getInstance().setUser(user);
        }
        String str = "兑换券剩余：" + user.CouponCount;
        tvCouponCount.setText(str);
        setData();
    }

    private void setData() {
        lotteryView.init(false, 1);
        lotteryView.setListener(new LotteryCircleView.LotteryListener() {
            @Override
            public void onSelected(ActivityBean.ActivityGiftBean activityGiftBean) {
                ToastUtils.getInstance().showInfo("恭喜获得" + activityGiftBean.GiftName);
            }

            @Override
            public void onSetReward() {
                lotteryView.setTargetGift(gifts.get(new Random().nextInt(gifts.size())));
            }

            @Override
            public void onGetCode() {
                User user = UserManage.getInstance().getUser();
                user.CouponCount -= 1;
                UserManage.getInstance().setUser(user);
                String str = "兑换券剩余：" + user.CouponCount;
                tvCouponCount.setText(str);
                lotteryView.setGetCodeAndStartFlash();
            }
        });
        gifts = new ArrayList<>();
        String[] names = {"小创客4件套", "定制玩偶", "6888金币", "马克笔套装", "乐高模型积木", "护眼灯", "文具套装", "米兔儿童手表"};
        String[] pics = {"http://resimg.iqeq.cn/myenglish/res/2021/1/29/dFJytt7itYrzKZYzCw4yKzZCWchM5RRC.png", "http://resimg.iqeq.cn/myenglish/res/2021/1/29/JsbjwaHFXHfMfWKe3Y745zSZrTXZB66D.png", "http://resimg.iqeq.cn/myenglish/res/2021/1/29/jmCWfQn5f5C6sGXW5CfWjszKXRndRZA6.png", "http://resimg.iqeq.cn/myenglish/res/2021/1/29/RQWdrnDdHHYfQsYwHDTpxT7tDriHc68G.png", "http://resimg.iqeq.cn/myenglish/res/2021/1/29/CsenMG8RPMnnQET5aXRctBKBKe6pfJaW.png", "http://resimg.iqeq.cn/myenglish/res/2021/1/29/FbG2iRBDipRmFiGGJRXMBrpHa6TZMJtE.png", "http://resimg.iqeq.cn/myenglish/res/2021/1/29/CXkX5H2kAK5kTWPSNApybyJt3rHmHTXr.png", "http://resimg.iqeq.cn/myenglish/res/2021/1/29/mdby2sBiSQZjNiw2be8mFHaDB6stcFhf.png"};
        for (int i = 0; i < 8; i++) {
            ActivityBean.ActivityGiftBean g = new ActivityBean.ActivityGiftBean();
            g.CouponCount = 1;
            g.GiftId = i;
            g.GiftName = names[i];
            g.GiftPic = pics[i];
            gifts.add(g);
        }
        lotteryView.setData(gifts);
    }

    @OnClick({R.id.tv})
    void click(View view) {
        switch (view.getId()) {
            case R.id.tv:
                User user = UserManage.getInstance().getUser();
                user.CouponCount += 1;
                UserManage.getInstance().setUser(user);
                String str = "兑换券剩余：" + user.CouponCount;
                tvCouponCount.setText(str);
                break;
            default:
        }
    }
}
